package org.igeek.atomqq.widget;

import org.igeek.atomqq.AtomQQApplication;
import org.igeek.atomqq.R;
import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.domain.Group;
import org.igeek.atomqq.net.Response;
import org.igeek.atomqq.task.FaceTask;
import org.igeek.atomqq.task.GroupMemoTask;
import org.igeek.atomqq.task.FaceTask.FaceResponse;
import org.igeek.atomqq.task.GroupMemoTask.GroupMemoResponse;
import org.igeek.atomqq.task.TaskResultListener;
import org.igeek.atomqq.util.Utils;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/** 
 * 群
 * @author <a href="http://hangxin1940.cnblogs.com">hangxin1940@gmail.com</a>
 * @time 2012-2-5 下午06:43:55 
 */
public class GroupItemView extends LinearLayout implements TaskResultListener {
	private BaseActivity context;
	
	private static final int FLAG_MEMO=0x0;
	private static final int FLAG_FACE=0x1;
	
	private Group group;
	private AlwaysMarqueeTextView amtvMarkName;
	private AlwaysMarqueeTextView amtvNickName;
	private AlwaysMarqueeTextView amtvMemo;
	private FriendFaceBadgeView ffbvBadge;
	private ImageView ivFace;
	
	private static long currentUin=0;
	
	
	private boolean flash=false;
	//闪烁
	private Handler handler=new Handler(){
		@Override
		public void handleMessage(Message msg) {
			
			if(flash){
				amtvMarkName.setVisibility(View.VISIBLE);
				amtvNickName.setVisibility(View.VISIBLE);
				amtvMemo.setVisibility(View.VISIBLE);
				ffbvBadge.setVisibility(View.VISIBLE);
				
				flash=false;
				handler.sendEmptyMessageDelayed(0, 300);
			}else{
				amtvMarkName.setVisibility(View.INVISIBLE);
				amtvNickName.setVisibility(View.INVISIBLE);
				amtvMemo.setVisibility(View.INVISIBLE);
				ffbvBadge.setVisibility(View.INVISIBLE);
				
				flash=true;
				handler.sendEmptyMessageDelayed(0, 500);
			}
			
		}
	};
	
	public GroupItemView(BaseActivity context) {
		super(context);
		
		if(currentUin==0)
			currentUin=((AtomQQApplication)context.getApplication()).getCurrentUser().getUin();
		
		
		
		//填充布局
		LayoutInflater infater=(LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		infater.inflate(R.layout.widget_group_item, this, true);
		
		this.context=context;
		
		amtvMarkName=(AlwaysMarqueeTextView) findViewById(R.id.widget_group_item_amtvGroupMarkname);
		amtvMemo=(AlwaysMarqueeTextView) findViewById(R.id.widget_group_item_amtvGroupLNick);
		amtvNickName=(AlwaysMarqueeTextView) findViewById(R.id.widget_group_item_amtvGroupname);
		ffbvBadge=(FriendFaceBadgeView) findViewById(R.id.widget_group_item_badge);
		ivFace=(ImageView) findViewById(R.id.widget_group_item_ivFace);
		
		handler.removeMessages(0);
	}

	public void setGroup(Group group) {
		this.group = group;
		
		
		
		String nickName=group.getName();
		String markName=group.getMarkname();
		String memo=group.getMemo();
		
		amtvNickName.setText("["+nickName+"]");
		
		if(null==markName||"".equals(markName)){
			amtvMarkName.setText(nickName);
			amtvNickName.setText("");
		}else{
			amtvMarkName.setText(markName);
		}
		
		
		
		
		if(null==memo){
			amtvMemo.setText("");
			GroupMemoTask taskMemo=new GroupMemoTask(context);
			taskMemo.setOnTaskResult(this, FLAG_MEMO);
			taskMemo.setParams(group.getCode());
			taskMemo.execute();
		}else{
			amtvMemo.setText(group.getMemo());
		}
		
		String face=Utils.getGroupFacePath(currentUin, group.getCode());
		
		if("".equals(face)){
			
			ivFace.setImageResource(R.drawable.groupface);
			FaceTask taskFace=new FaceTask(context);
			
			taskFace.setOnTaskResult(this, FLAG_FACE);
			taskFace.setParams(currentUin, group.getCode(), FaceTask.GROUP_FACE);
			taskFace.execute();
		}else if("sderr".equals(face)){
			//sd卡读不在的情况下
			ivFace.setImageResource(R.drawable.groupface);
		}else{
			ivFace.setImageURI(Uri.parse(face));
		}
	
		/**闪烁*/
		if(group.hasNewMessage()==1){
			flash=true;
			handler.sendEmptyMessageDelayed(0, 300);
		}else{
			flash=false;
			handler.removeMessages(0);
			amtvMarkName.setVisibility(View.VISIBLE);
			amtvNickName.setVisibility(View.VISIBLE);
			amtvMemo.setVisibility(View.VISIBLE);
			ffbvBadge.setVisibility(View.VISIBLE);
			
		}
		
		
	}

	public Group getGroup() {
		return group;
	}
	
	public View getBadgeView(){
		return ffbvBadge;
	}

	@Override
	public void onTaskResult(Response response, int flag) {
		if(null==response)
			return;
		
		switch (flag) {
		case FLAG_MEMO:
			Group memo=((GroupMemoResponse)response).getGroupMemo();
			
			((AtomQQApplication)context.getApplication()).getGroup(memo.getCode()).setMemo(memo.getMemo());
			
			if(memo.getCode()==group.getCode()){
				amtvMemo.setText(memo.getMemo());
			}
			
			break;

		case FLAG_FACE:
			Group face=((FaceResponse)response).getGroup();
			String facePath=face.getFace();
			
			if(face.getCode()==group.getCode()){
				ivFace.setImageURI(Uri.parse(facePath));
					
			}
			
			break;
		}
		
		
		
		
		
	}

}
